﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Sql;
using System.Data.SqlClient;
using System.Data;
using DevExpress.Web.ASPxGridView;
using System.Text;

public partial class PrimOran : System.Web.UI.Page
{
    private DcSql conn, connMikroDB;
    private string VT_Prim = "Prim";
    private string VT_MikroDB = "MikroDB";

    protected void Page_Load(object ssender, EventArgs e)
    {
        BindGrid();
    }

    private void BindGrid()
    {
        conn = new DcSql();
        string HataMesaji = conn.DBBaglan(VT_Prim);

        connMikroDB = new DcSql();
        HataMesaji = connMikroDB.DBBaglan(VT_MikroDB);

        string selectsql = "select top 30 sto_RECno,sto_kod,sto_isim from STOKLAR where sto_kod IS NOT NULL order by sto_isim ";
        DataSet dsMikro = new DataSet();
        SqlDataAdapter daMikro = new SqlDataAdapter(selectsql, connMikroDB.GetConn());

        daMikro.Fill(dsMikro, "STOKLAR");

        GridViewDataComboBoxColumn combo = gridPrimOran.Columns["psth_stok_kod"] as GridViewDataComboBoxColumn;

        combo.PropertiesComboBox.DataSource = dsMikro.Tables[0];
        combo.PropertiesComboBox.ValueField = "sto_kod";
        combo.PropertiesComboBox.ValueType = typeof(int);
        combo.PropertiesComboBox.TextField = "sto_isim";

        if (conn.durum == false) { return; }
        Yenile();
    }


    private void Yenile()
    {
        string selectsql = "SELECT * FROM [PrimOran]";

        DataSet ds = new DataSet();
        SqlDataAdapter da = new SqlDataAdapter(selectsql, conn.GetConn());

        da.Fill(ds, "PrimOran");

        gridPrimOran.DataSource = ds.Tables[0];

        if (!IsPostBack && !IsCallback)
            gridPrimOran.DataBind();
    }

    protected void ASPxGridView1_RowDeleting(object sender, DevExpress.Web.Data.ASPxDataDeletingEventArgs e)
    {
        SqlCommand cmd = new SqlCommand("PrimOranSil", conn.GetConn());
        cmd.CommandType = CommandType.StoredProcedure;

        ASPxGridView gridView = (ASPxGridView)sender;
        int index = gridView.FindVisibleIndexByKeyValue(e.Keys[gridView.KeyFieldName]);

        string primOranId = gridView.GetRowValues(index, gridView.KeyFieldName).ToString();
        cmd.Parameters.AddWithValue("@PrimOranID", primOranId);
        cmd.ExecuteScalar();

        e.Cancel = true;
        gridView.CancelEdit();
        Yenile();
    }

    protected void ASPxGridView1_RowUpdating(object sender, DevExpress.Web.Data.ASPxDataUpdatingEventArgs e)
    {
        SqlCommand cmd = new SqlCommand("PrimOranDuzelt", conn.GetConn());
        cmd.CommandType = CommandType.StoredProcedure;

        ASPxGridView gridView = (ASPxGridView)sender;
        e.NewValues["PrimOranID"] = gridView.GetRowValues(gridView.EditingRowVisibleIndex, "PrimOranID").ToString();

        cmd.Parameters.AddWithValue("@PrimOranID", e.NewValues["PrimOranID"]);
        cmd.Parameters.AddWithValue("@psth_stok_kod", e.NewValues["psth_stok_kod"]);
        cmd.Parameters.AddWithValue("@nFiyat1", e.NewValues["nFiyat1"]);
        cmd.Parameters.AddWithValue("@nFiyat1Bitis", e.NewValues["nFiyat1_Bitis"]);
        cmd.Parameters.AddWithValue("@nFiyat2", e.NewValues["nFiyat2"]);
        cmd.Parameters.AddWithValue("@nFiyat2Bitis", e.NewValues["nFiyat2_Bitis"]);
        cmd.Parameters.AddWithValue("@nFiyat3", e.NewValues["nFiyat3"]);
        cmd.Parameters.AddWithValue("@nFiyat3Bitis", e.NewValues["nFiyat3_Bitis"]);
        cmd.Parameters.AddWithValue("@nFiyat4", e.NewValues["nFiyat4"]);
        cmd.Parameters.AddWithValue("@nFiyat4Bitis", e.NewValues["nFiyat4_Bitis"]);
        cmd.Parameters.AddWithValue("@nFiyat5", 0);
        cmd.Parameters.AddWithValue("@nFiyat5Bitis", 0);
        cmd.ExecuteScalar();

        e.Cancel = true;
        gridView.CancelEdit();
        Yenile();
    }

    protected void ASPxGridView1_RowInserting(object sender, DevExpress.Web.Data.ASPxDataInsertingEventArgs e)
    {
        SqlCommand cmd = new SqlCommand("PrimOranEkle", conn.GetConn());
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@psth_stok_kod", e.NewValues["psth_stok_kod"]);
        cmd.Parameters.AddWithValue("@nFiyat1", e.NewValues["nFiyat1_Bitis"]);
        cmd.Parameters.AddWithValue("@nFiyat1Bitis", e.NewValues["nFiyat1_Bitis"]);
        cmd.Parameters.AddWithValue("@nFiyat2", e.NewValues["nFiyat2_Bitis"]);
        cmd.Parameters.AddWithValue("@nFiyat2Bitis", e.NewValues["nFiyat2_Bitis"]);
        cmd.Parameters.AddWithValue("@nFiyat3", e.NewValues["nFiyat3_Bitis"]);
        cmd.Parameters.AddWithValue("@nFiyat3Bitis", e.NewValues["nFiyat3_Bitis"]);
        cmd.Parameters.AddWithValue("@nFiyat4", e.NewValues["nFiyat4_Bitis"]);
        cmd.Parameters.AddWithValue("@nFiyat4Bitis", e.NewValues["nFiyat4_Bitis"]);
        cmd.Parameters.AddWithValue("@nFiyat5", 0);
        cmd.Parameters.AddWithValue("@nFiyat5Bitis",0);
        cmd.ExecuteScalar();

        e.Cancel = true;
        gridPrimOran.CancelEdit();
        Yenile();
    }
    protected void gridPrimOran_RowValidating(object sender, DevExpress.Web.Data.ASPxDataValidationEventArgs e)
    {
        int i = 1;
    }
}